﻿<template>
	<div class="equipInfo-container">
		<el-dialog v-model="isShowDialog" :title="props.title" :width="700" draggable="">
			<el-form :model="ruleForm" ref="ruleFormRef" size="default" label-width="100px" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="名称" prop="name">
							<el-input v-model="ruleForm.name" placeholder="请输入名称" clearable />
							
						</el-form-item>
						
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="编号" prop="num">
							<el-input  v-model="ruleForm.num" placeholder="请输入编号" clearable />
							
						</el-form-item>
						
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="单元号" prop="unitnum">
							<el-input v-model="ruleForm.unitnum" placeholder="请输入单元号" clearable />
							
						</el-form-item>
						
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="绑定号" prop="bindnum">
							<el-input v-model="ruleForm.bindnum" placeholder="请输入绑定号" clearable />
							
						</el-form-item>
						
					</el-col>


					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="分组" prop="groupname">
							<el-input v-model="ruleForm.groupname" @click='openGroup' placeholder="请输入分组" clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="程序" prop="programid">
							
							<el-select v-model="ruleForm.programid" class="m-2" placeholder="选择" >
							<el-option v-for="item in equipInfos" :key="item.id" :label="item.name" :value="item.id" />
						    </el-select>
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="IP端口" prop="ipaddress">
							<el-input v-model="ruleForm.ipaddress" placeholder="请输入IP端口" clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="是否启用" prop="isenable">
							
							<el-select v-model="ruleForm.isenable" class="m-2" placeholder="选择" >
							<el-option  label="启用" :value="1" />
							<el-option  label="禁用" :value="0" />
						    </el-select>
						</el-form-item>
					</el-col>
					<!--
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="Mqtt发布" prop="ismqttpublish">
							
							<el-select v-model="ruleForm.ismqttpublish" class="m-2" placeholder="选择" >
							<el-option  label="是" :value="1" />
							<el-option  label="否" :value="0" />
						    </el-select>
						</el-form-item>
					</el-col>
					-->
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="轮询时间" prop="pooling">
							<el-input-number :step="500" min="500" v-model="ruleForm.pooling" placeholder="请输入轮询时间" clearable />
							
						</el-form-item>
						
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="序号" prop="ordernum">
							<el-input-number :step="1" min="0" v-model="ruleForm.ordernum" placeholder="请输入序号" clearable />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="设备类型" prop="equiptype">
							
							<el-select filterable v-model="ruleForm.equiptype" class="m-2" placeholder="选择" >
							<el-option v-for="item in equipTypes" :key="item.item1" :label="item.item2" :value="item.item1" />
						</el-select>
						</el-form-item>
						
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="协议类型" prop="equiptype">
							
							<el-select filterable v-model="ruleForm.protobuftype" class="m-2" placeholder="选择" >
							<el-option v-for="item in protoTypes" :key="item.item1" :label="item.item1" :value="item.item1" />
						</el-select>
						</el-form-item>
						
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12"  class="mb20">
						<el-form-item label="解析方向" prop="readdirection">
							<el-input v-model="ruleForm.readdirection" placeholder="请输入解析方向" clearable />							
						</el-form-item>
						
					</el-col>

					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12"  class="mb20">
						<el-form-item label="备注" prop="remark">
							<el-input v-model="ruleForm.remark" placeholder="请输入备注" clearable />
							
						</el-form-item>
						
					</el-col>
					
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel" size="default">取 消</el-button>
					<el-button type="primary" @click="submit" size="default">确 定</el-button>
				</span>
			</template>
		</el-dialog>

		<equipGroupModal ref='groupModal' @close='closeGroupModal'/>
	</div>
</template>

<script lang="ts" setup>
	import { ref,onMounted } from "vue";
	import { ElMessage } from "element-plus";
	import type { FormRules } from "element-plus";
	import { addEquipInfo, updateEquipInfo } from "/@/api/main/equipInfo";

	import equipGroupModal from '/@/views/main/equipGroup/component/equipGroupModal.vue'
	import { getProgramList } from '/@/api/main/programAll';
import { debug } from "console";

	
	//父级传递来的参数
	var props = defineProps({
	title: {
	type: String,
	default: "",
	},
	});
	const groupModal=ref();
	//父级传递来的函数，用于回调
	const emit = defineEmits(["reloadTable"]);
	const ruleFormRef = ref();
	const isShowDialog = ref(false);
	const ruleForm = ref<any>({});
		//自行删除非必填规则
		const rules = ref<FormRules>({
  id: [{required: true, message: '请输入Id！', trigger: 'blur',},],
  name: [{required: true, message: '请输入名称！', trigger: 'blur',},],
  groupid: [{required: true, message: '请选择分组！', trigger: 'blur',},],
  
  ipaddress: [{required: true, message: '请输入IP端口！', trigger: 'blur',},],
  isenable: [{required: true, message: '请输入是否启用！', trigger: 'blur',},],
  num: [{required: true, message: '请输入编号！', trigger: 'blur',},],
  pooling: [{required: true, message: '请输入轮询时间！', trigger: 'blur',},],
  equiptype: [{required: true, message: '请输入设备类型！', trigger: 'blur',},],
  
 
});
const equipInfos=ref<any>([]);
const equipTypes=ref<any>([]);
const protoTypes=ref<any>([]);
// 打开弹窗
const openDialog = (row: any,equips:[],types:[],protobufs:[]) => {

  
  ruleForm.value = JSON.parse(JSON.stringify(row));

  isShowDialog.value = true;

  equipInfos.value=equips;
  equipTypes.value=types;
  protoTypes.value=protobufs;

};

// 关闭弹窗
const closeDialog = () => {
  emit("reloadTable");
  isShowDialog.value = false;
};

// 取消
const cancel = () => {
  isShowDialog.value = false;
};

// 提交
const submit = async () => {
  ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
    if (isValid) {
      let values = ruleForm.value;
	  
      if (ruleForm.value.id != undefined && ruleForm.value.id > 0) {
        await updateEquipInfo(values);
      } else {
        await addEquipInfo(values);
      }
      closeDialog();
    } else {
      ElMessage({
        message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
        type: "error",
      });
    }
  });
};

const closeGroupModal=(row)=>{

	ruleForm.value.groupname=row.name;
	ruleForm.value.groupid=row.id;
}

const openGroup=()=>{
	groupModal.value.open();
}



// 页面加载时
onMounted(async () => {
});

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>




